package com.tiancheng.trade.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiancheng.trade.commom.web.model.response.QueryAreaRes;
import com.tiancheng.trade.merchant.convert.AreaConvert;
import com.tiancheng.trade.merchant.mapper.AreaMapper;
import com.tiancheng.trade.merchant.model.Area;
import com.tiancheng.trade.merchant.service.IAreaService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements IAreaService {

    @Override
    public List<QueryAreaRes> queryByParentAreaCode(String parentAreaCode) {
        List<Area> list = this.list(new LambdaQueryWrapper<Area>().eq(Area::getParentAreaCode, parentAreaCode));
        if(CollectionUtils.isNotEmpty(list)){
            return AreaConvert.INSTANCE.convert(list);
        }
        return null;
    }

    @Override
    public List<QueryAreaRes> queryByAreaCode(String parentAreaCode) {
        List<String> areaCods=new ArrayList<>();
        for (String s : parentAreaCode.split(",")) {
            if(StringUtils.isNotEmpty(s)){
                areaCods.add(s);
            }
        }

        List<Area> list = this.list(new LambdaQueryWrapper<Area>().in(Area::getAreaCode, areaCods));
        if(CollectionUtils.isNotEmpty(list)){
            return AreaConvert.INSTANCE.convert(list);
        }
        return null;
    }
}
